package com.dmwork.service.message.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.message.entity.SystemMessage;
import com.dmwork.service.message.service.SystemMessageService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 系统通知消息表 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-06-01
 */
@Slf4j
@Transactional
@RestController
@RequestMapping("/front/dm_work/message/system_message/me")
public class FrontMeSystemMessageController {

    @Autowired
    private SystemMessageService systemMessageService;

    @ApiOperation("获取未读的系统消息数")
    @GetMapping("get_unread_count")
    public R getUnreadCount(HttpServletRequest request) {
        log.error(request.getHeader("token"));
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        int count = systemMessageService.count(new QueryWrapper<SystemMessage>().eq("customer_id", jwtInfo.getId()).eq("read_status", 0));
        return R.ok().data("count", count);
    }

    @ApiOperation("获取平台系统消息")
    @GetMapping("get_system_message/{page}/{limit}")
    public R getSystemMessage(HttpServletRequest request,  @PathVariable Integer page, @PathVariable Integer limit) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        Page<SystemMessage> systemMessagePage =  systemMessageService.getSystemMessage(jwtInfo.getId(), page, limit);
        return R.ok().data("total", systemMessagePage.getTotal()).data("rows", systemMessagePage.getRecords());
    }

}